package com.btcode.web.core.unit;

import freemarker.template.utility.DateUtil.DateParseException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;

public class RequestUnit {

    static List<String> parsePatterns = Arrays.asList("yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss",
                                                      "yyyy-MM-dd HH:mm", "yyyy/MM/dd HH:mm",
                                                      "yyyy-MM-dd", "yyyy/MM/dd");

    public static String getString(String paramName) {
        String value = getParameter(paramName);
        return value;
    }

    /**
     * 获取逗号分隔的数组
     */
    public static String[] getStringArray(String paramName) {
        String value = getParameter(paramName);
        if (value == null) {
            return null;
        }
        String[] result = value.split(",");
        return result;
    }

    /**
     * 获取标准的数组
     */
    public static String[] getArray(String paramName) {
        String[] values = WebContext.getRequest().getParameterValues(paramName + "[]");
        return values;
    }

    public static Integer getInt(String paramName) {
        try {
            int value = Integer.parseInt(getParameter(paramName).trim());
            return value;
        }
        catch (Exception e) {
            return null;
        }
    }

    public static BigDecimal getBigDecimal(String paramName) {
        try {
            double dValue = getDouble(paramName);
            BigDecimal value = new BigDecimal(dValue);
            return value;
        }
        catch (Exception e) {
            return null;
        }
    }

    public static Double getDouble(String paramName) {
        try {
            double value = Double.parseDouble(getParameter(paramName));
            return value;
        }
        catch (Exception e) {
            return null;
        }
    }

    public static Timestamp getTimestamp(String paramName) {
        try {
            String valueStr = getParameter(paramName);
            Timestamp value = parseDate(valueStr);
            return value;
        }
        catch (Exception e) {
            return null;
        }
    }

    public static HashMap<String, Object> getHashMapParmO() {
        Enumeration<String> names = WebContext.getRequest().getParameterNames();
        try {
            WebContext.getRequest().setCharacterEncoding("utf-8");
        }
        catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        HashMap<String, Object> parm = new HashMap<>();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            String value = getString(name);
            parm.put(name, value);
        }
        return parm;
    }

    public static HashMap<String, String> getHashMapParmS() {
        Enumeration<String> names = WebContext.getRequest().getParameterNames();
        HashMap<String, String> parm = new HashMap<>();
        String waterType = RequestUnit.getString("WATER_TYPE");
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            String value = getString(name);
            parm.put(name, value);
        }
        parm.put("WATER_TYPE", waterType);
        return parm;
    }

    public static String getParameter(String paramName) {
        String result = WebContext.getRequest().getParameter(paramName);
        if (result != null) {
            return result;
        }

        return result;
    }

    public static void setAttr(String key, Object value) {
        WebContext.getRequest().setAttribute(key, value);
    }

    public static Timestamp parseDate(String dateStr) throws DateParseException {
        for (String formatStr : parsePatterns) {

            SimpleDateFormat df = new SimpleDateFormat(formatStr);
            try {
                Date date = df.parse(dateStr);
                Timestamp value = new Timestamp(date.getTime());
                return value;
            }
            catch (Exception e) {
            }
        }
        return null;
    }
}
